//
//  ST_ST_SwiftyFloatingWidgetProtocolProtocol.swift
//  
//
//  Created by Jason on 2021/8/10.
//

import Foundation
import UIKit


//MARK: - Protocol
public protocol ST_ST_SwiftyFloatingWidgetProtocol where Self : UIViewController {
    /// 提供一个可共享的View
    func swiftyFloatingShareView() -> UIView
    
    /// 准备浮动到Frame
    func swiftyPrepareFloat(toFrame:CGRect)
    /// 浮动动画
    func swiftyFloatAnimating(toFrame:CGRect)
    /// 浮动动画完成
    func swiftyFloatAnimationComplete()
    /// 能否从浮动状态还原
    func swiftyFloatingCanRecover()->Bool
    /// 准备从浮动状态还原
    func swiftyPrepareRecover(recoverRect:CGRect,completion:@escaping (() -> Void))
    /// 从浮动状态还原动画
    func swiftyRecovering()
    /// 还原完成
    func swiftyRecoverComplete()
}

//MARK: - Providing Default Implementations , 默认实现
public extension ST_ST_SwiftyFloatingWidgetProtocol{
    
    
    func swiftyPrepareFloat(toFrame: CGRect) {
        
        view.isUserInteractionEnabled = false
       
        
        self.swiftyFloatingShareView().layer.masksToBounds = true
        self.swiftyFloatingShareView().layer.cornerRadius = abs(CGFloat(Int(ST_SwiftyWidgetConfig.default.floatingWindowRadious * 100)) / 100)
        
    }
    
    func swiftyFloatAnimating(toFrame: CGRect) {

        view.alpha = 0
        
        let t = CGAffineTransform.transform(from: UIScreen.main.bounds, to: toFrame)
        
        view.transform = t
    }
    
    func swiftyFloatAnimationComplete() {
        
        if let nav = self.navigationController,let firstVC = nav.viewControllers.first{
            
            if firstVC == self{
                
                nav.dismiss(animated: false, completion: nil)
                
            }else{
                
                nav.popViewController(animated: false)
                
            }
            
        }else{
            
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    func swiftyFloatingCanRecover()->Bool{
        return true
    }
    
    func swiftyPrepareRecover(recoverRect: CGRect, completion: @escaping (() -> Void)) {
        
        self.swiftyFloatingShareView().layer.cornerRadius = 0
        
        guard var rootController = UIApplication.shared.windows.first?.rootViewController else{
            return
        }
        
        
        while rootController.presentedViewController != nil {
            
            if let presented = rootController.presentedViewController{
                rootController = presented
            }
            
        }
        
        
        rootController.present(self, animated: false) {
            
            let recoverTransform = CGAffineTransform.transform(from: UIScreen.main.bounds, to: recoverRect)
            
            self.view.bounds = UIScreen.main.bounds

            self.view.center = CGPoint(x: UIScreen.main.bounds.size.width/2.0, y: UIScreen.main.bounds.size.height/2.0)
            
            self.view.transform = recoverTransform
            
            self.view.layoutIfNeeded()
            
            completion()
        }
    }
    
    func swiftyRecovering() {
        
        view.alpha = 1
        
        view.transform = .identity
        
    }
    
    func swiftyRecoverComplete() {
        
        view.isUserInteractionEnabled = true
        
    }
    
    
}

